package com.bulletelc.apollopre.module.main.center.collection;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.CollectionBean;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.module.display.product.details.ProductDetailsActivity;

import java.util.List;

/**
 * Created by CX on 2018/1/25.
 * 收藏列表adapter
 */

public class CollectionsListAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<CollectionBean> collectionList;
    private LayoutInflater inflater;

    private OnClickListener onClickListener;

    public CollectionsListAdapter(Context context, List<CollectionBean> list) {
        this.context = context;
        this.collectionList = list;
        inflater = LayoutInflater.from(context);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public int getCount() {
        return collectionList.size();
    }

    @Override
    public Object getItem(int position) {
        return collectionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        CollectionBean collection = collectionList.get(i);
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.collection_list_item, parent, false);
            holder.llContent = view.findViewById(R.id.llContent);
            holder.ivSmallImg = view.findViewById(R.id.iv_small_img);
            holder.tvName = view.findViewById(R.id.tv_name);
            holder.tvPrice = view.findViewById(R.id.tv_price);
            holder.tvStatus = view.findViewById(R.id.tv_status);
            holder.btnRemove = view.findViewById(R.id.btn_remove);

            holder.llContent.setOnClickListener(this);
            holder.btnRemove.setOnClickListener(this);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holder.ivSmallImg, collection.getProduct_avatar()));
        holder.tvName.setText(collection.getProduct_name());
        holder.tvPrice.setText("¥" + collection.getSales_price() == null ? "" : collection.getSales_price());
        holder.tvStatus.setText(collection.getStatus() == 1 ? "有货" : "缺货");

        holder.btnRemove.setTag(i);
        holder.llContent.setTag(i);

        return view;
    }

    @Override
    public void onClick(View view) {
        final int position = (int) view.getTag();
        String productId = collectionList.get(position).getProduct_id();
        switch (view.getId()) {
            case R.id.btn_remove:
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setMessage("确定删除吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (onClickListener != null) {
                                    onClickListener.onClick(position);
                                }
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
                break;
            case R.id.llContent:
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_PRODUCT_ID, productId);
                context.startActivity(intent);
                break;
        }
    }

    class ViewHolder {
        LinearLayout llContent;
        TextView tvName;
        TextView tvPrice;
        TextView tvStatus;
        ImageView ivSmallImg;
        ImageButton btnRemove;
    }

    interface OnClickListener {

        void onClick(int position);
    }
}
